package plugincleaner.preferences;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Set;

import model.PluginCleaner;
import model.PluginCleanerFactory;

import org.eclipse.jface.preference.*;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;

import exception.DeleteFailureException;

import plugincleaner.Activator;

public class PluginCleanerPreferencePage extends FieldEditorPreferencePage
		implements IWorkbenchPreferencePage, PluginTableController {

	private MyDirectoryFieldEditor gMyDirectoryFieldEditor;
	private MyTableFieldEditor gLatestPluginTable;
	private MyTableFieldEditor gOldPluginTable;
	private MyTableFieldEditor gMalformedPluginTable;
	private MyCleanButtonFieldEditor gCleanButton;

	private PluginCleaner gCleaner;

	public PluginCleanerPreferencePage() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("Clean up Eclipse old plugins");
		noDefaultAndApplyButton();
	}

	public void createFieldEditors() {
		addField(gMyDirectoryFieldEditor = new MyDirectoryFieldEditor(
				PreferenceConstants.P_PATH, "plugins/features path:",
				getFieldEditorParent(), this));
		addField(gLatestPluginTable = new MyTableFieldEditor(
				PreferenceConstants.P_FOUND_LATEST_PLUGINS,
				"Plugins with versions: ", getFieldEditorParent(), true, this));

		addField(gOldPluginTable = new MyTableFieldEditor(
				PreferenceConstants.P_FOUND_OLD_PLUGINS, "",
				getFieldEditorParent(), false, this));

		addField(gMalformedPluginTable = new MyTableFieldEditor(
				PreferenceConstants.P_MALFORMED_PLUGINS, "Malformed plugins: ",
				getFieldEditorParent(), false, this));

		addField(gCleanButton = new MyCleanButtonFieldEditor(
				PreferenceConstants.P_MALFORMED_PLUGINS, "",
				getFieldEditorParent(), this));
	}

	public void init(IWorkbench workbench) {
	}

	@Override
	public void updateFoundPluginTable(String startPath) {
		// GET PLUGINS
		this.gCleaner = PluginCleanerFactory.getInstance().getPluginCleaner();
		this.gCleaner.readAllPlugins(startPath);

		Hashtable<String, ArrayList<String>> normalPlugins = this.gCleaner
				.getGFilenames();

		Set<String> keys = normalPlugins.keySet();
		for (String key : keys) {
			ArrayList<String> fns = normalPlugins.get(key);
			if (fns.size() > 1) {
				this.gLatestPluginTable.addItem(fns.get(0));
			}
		}

		ArrayList<String> malformedPlugins = this.gCleaner
				.getGCantRecorgFilenames();
		for (String s : malformedPlugins) {
			this.gMalformedPluginTable.addItem(s);
		}

	}

	@Override
	public void updateOldPluginTable(String plugin) {
		this.gOldPluginTable.removeAll();
		ArrayList<String> plugins = this.gCleaner.getGFilenames().get(
				plugin.substring(0, plugin.indexOf('_')));
		for (int i = 1; i < plugins.size(); i++) {
			this.gOldPluginTable.addItem(plugins.get(i));
		}
	}

	@Override
	public void cleanAll() {
		this.gLatestPluginTable.removeAll();
		this.gMalformedPluginTable.removeAll();
		this.gOldPluginTable.removeAll();
	}

	@Override
	public void removeSelectedPlugins() {
		ArrayList<String> plugins = this.gLatestPluginTable.getSelectedItems();

		if (plugins == null || plugins.size() == 0) {
			return;
		}

		Hashtable<String, ArrayList<String>> toRemove = new Hashtable<String, ArrayList<String>>();
		for (String s : plugins) {
			String key = s.substring(0, s.indexOf('_'));
			toRemove.put(key, this.gCleaner.getGFilenames().get(key));
		}

		try {
			PluginCleaner.removeOldPlugins(this.gCleaner.getGStartPath(),
					toRemove);
		} catch (DeleteFailureException e) {
			e.printStackTrace();
		}

		toRemove = null;
		plugins = null;

		// update table
		cleanAll();
		updateFoundPluginTable(this.gCleaner.getGStartPath());
	}

	@Override
	public void removeAllPlugins() {
		try {
			PluginCleaner.removeOldPlugins(this.gCleaner.getGStartPath(),
					this.gCleaner.getGFilenames());
		} catch (DeleteFailureException e) {
			e.printStackTrace();
		}

		// update table
		cleanAll();
		updateFoundPluginTable(this.gCleaner.getGStartPath());
	}
}
